package com.dcivision.dms.client;
/*
 * @(#)DmsErrorConstant.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

import java.io.File;
import java.util.Hashtable;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.InputSource;

import com.dcivision.dms.client.parser.IndexParser;

/**
  GlobalIndexTransformation.java -- an application that reads an Hashtable object, to define
  the target index parser to be used, and generate DCIVision index file format for DmsClientRun.class delivering.

    @author          Zoe Shum
    @company         DCIVision Limited
    @creation date   06/09/2003
    @version         $Revision: 1.15.2.1 $
*/
public class GlobalIndexTransformation{

  public static final String REVISION = "$Revision: 1.15.2.1 $";

  public static final String STATUS_ERROR="error";

  private static final String ACME_SCAN_FORMAT = "ACME";
  private static final String KODAK_SCAN_FORMAT = "KODAK";
  private static final String KONICA_SCAN_FORMAT = "KONICA";
  private static final String BIZSOFT_SCAN_FORMAT = "BIZSOFT";
  private static final String ACME_INDEX_READER_CLASS = "com.dcivision.dms.client.parser.ACMEIndexParser";
  private static final String KONICA_INDEX_READER_CLASS = "com.dcivision.dms.client.parser.KONICAIndexParser";
  private static final String BIZSOFT_INDEX_READER_CLASS = "com.dcivision.dms.client.parser.BIZSOFTIndexParser";

  protected static String m_sScanFormat = "";
  protected static String m_sResult = "";
  protected IndexParser xmlReader = null;
  protected String uploadFileIndexExten = "";
  protected String documentIndexExten = "";
  protected String loginName="";
  protected String loginPwd="";
  protected String rootID="";
  protected String parentID="";
  
  private static final Log log = LogFactory.getLog(GlobalIndexTransformation.class);
  protected GlobalIndexTransformation() {
  }

  protected GlobalIndexTransformation(String scanFormat) {
    m_sScanFormat=scanFormat;

  }

  public void IndexTransformer (String uploadFile, Hashtable confTable) throws Exception{
    try{
      // Create the sax "parser".
      if("ACME".equals(confTable.get(IndexParser.SCAN_METHOD))) {
        this.xmlReader = (IndexParser)Class.forName(ACME_INDEX_READER_CLASS).newInstance();
      }
      else if("KONICA".equals(confTable.get(IndexParser.SCAN_METHOD))) {
        this.xmlReader = (IndexParser)Class.forName(KONICA_INDEX_READER_CLASS).newInstance();
      }
      else if("BIZSOFT".equals(confTable.get(IndexParser.SCAN_METHOD))) {
        this.xmlReader = (IndexParser)Class.forName(BIZSOFT_INDEX_READER_CLASS).newInstance();
      }

    } catch (ClassNotFoundException cne) {
      throw cne;
    }

    uploadFileIndexExten = (String)confTable.get(IndexParser.SCAN_UPLOAD_FILE_INDEX_EXTEN);
    documentIndexExten = (String)confTable.get(IndexParser.SCAN_DOCUMENT_INDEX_EXTEN);

    String indexFile = ConvertIndexFileName(uploadFile);
    confTable.put(IndexParser.OLD_INDEX_FILE_PATH, indexFile);

    m_sResult=ConvertResultFile(uploadFile);
    confTable.put(IndexParser.XML_INDEX_FILE_PATH, m_sResult);

    String xmlIndexFile = m_sResult.substring(m_sResult.lastIndexOf(File.separator),  m_sResult.length());
    confTable.put(IndexParser.XML_INDEX_FILENAME, xmlIndexFile);

    this.xmlReader.setConfigTable(confTable);

    try {

      // Use a Transformer for output
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer();

      // Use the parser as a SAX source for input
      InputSource inputSource = new InputSource(indexFile);
      SAXSource source = new SAXSource(xmlReader, inputSource);
      StreamResult result = new StreamResult(m_sResult);
      transformer.transform(source, result);

    } catch (TransformerConfigurationException tce) {
      // Error generated by the parser
      System.out.println ("\n** Transformer Factory error");
      System.out.println("   " + tce.getMessage() );

      // Use the contained exception, if any
      Throwable x = tce;
      if (tce.getException() != null)
        x = tce.getException();
      log.error(x, x);
      throw new Exception(x);
    } catch (TransformerException te) {
      // Error generated by the parser
      System.out.println ("\n** Transformation error");
      System.out.println("   " + te.getMessage() );

      // Use the contained exception, if any
      Throwable x = te;
      if (te.getException() != null)
        x = te.getException();
      log.error(x, x);
      throw new Exception(x);
    }

    this.loginName = this.xmlReader.getLoginName();
    this.loginPwd = this.xmlReader.getLoginPwd();
    this.rootID = this.xmlReader.getRootID();
    this.parentID = this.xmlReader.getParentID();
  }

  public void backupAllFiles(String actionStatus){
    this.xmlReader.backupAllFiles(actionStatus);
  }

  public String ConvertIndexFileName (String uploadFile) {
    String indexFile = String.valueOf(uploadFile.substring(0, uploadFile.length() - 4)).concat(uploadFileIndexExten);
System.out.println("initial index file = \t"+indexFile);
    return indexFile;
  }

  public String ConvertResultFile (String uploadFile) {
    String indexFile = String.valueOf(uploadFile.substring(0, uploadFile.length() - 4)).concat("dci").concat(documentIndexExten);
//    System.out.println("result index file ="+indexFile);
    return indexFile;
  }

  public String getLoginName(){
    return this.loginName;
  }

  public String getLoginPwd(){
    return this.loginPwd;
  }

  public String getRootID(){
    return this.rootID;
  }

  public String getParentID(){
    return this.parentID;
  }

  public static void main (String argv []){
   /* if (argv.length != 1) {
      System.err.println ("Usage: java TransformationApp filename");
      System.exit (1);
    }
    GlobalIndexTransformation transformer = new GlobalIndexTransformation();
    transformer.IndexTransformer(argv[0]);
*/
  } // main

}